package net.ba.models
{
	import com.adobe.serialization.json.JSON;
	
	import flash.xml.XMLDocument;
	import flash.xml.XMLNode;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.xml.SimpleXMLEncoder;
	
	[Bindable] 
	public class Dataset
	{
		public var id:Number;
		public var guid:String;
		public var title:String;
		public var author:User;
		public var modifiedWhen:Date;
		public var createdWhen:Date;
		public var totalOcc:Number=0;
		public var totalSpecies:Number=0;
		public var totalCells:Number=0;
		public var totalSources:Number=0;
		public var species:ArrayCollection; //Array of Species
	
		public function createNewSpecies(species_:Species):void {
			totalSpecies++;
			totalOcc = totalOcc + species_.totalOcc;
			totalCells = totalCells + species_.totalCells;
			species.addItem(species_);
		}
	
		public function updateSpecies(species_:Species):void {
			totalOcc = 0;
			totalCells =0;
			for each(var s:Species in species) {
				if (s.id == species_.id) {
					s.cellOccs = species_.cellOccs;
					s = species_;
				}
				totalOcc = totalOcc + s.totalOcc;
				totalCells = totalCells +  s.totalCells;
				
			}
		}
		
		public function getSpeciesById(id:Number):Species {
			for each(var s:Species in species) {
				if (s.id == id) {
					return s;
				}
			}	
			return null;		
		}
		
		public function getSpeciesIndexById(id_:Number):Number {
			var i:Number = -1;
			for each(var s:Species in species) {
				i++;
				if (s.id == id_) {
					return i;
				}
			}	
			return -1;					
		}
		
		public function getNextFreeSpeciesId():Number {
			var maxSpeciesId:Number = 0;
			for each(var s:Species in species) {
				if (s.id > maxSpeciesId) {
					maxSpeciesId = s.id;
				}
			}	
			return maxSpeciesId + 1;
		}
		
		public function toXML():String {
			var doc:XMLDocument = new XMLDocument('<data></data>');
		  	var simpleEncode:SimpleXMLEncoder = new SimpleXMLEncoder(doc);
		  	var node:XMLNode = simpleEncode.encodeValue(this, new QName('http://biodiversityatlas.com','ba'),doc.firstChild);
		  	var xml:XML = new XML(doc)
		  	var header:String = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!-- Document generated from biodiversityatlas.com Dataset Editor -->\n";
		  	return(header + xml.toString());
		}
		
		public function toJSON():String {
			
			return JSON.encode(this);
		}
		
		public function fromXML(xml:XML):void {
			id = xml.ba.id;
			//createdWhen = xml.ba.createdWhen as Date;
			title = xml.ba.title;
			totalCells = xml.ba.totalCells;
			totalOcc = xml.ba.totalOcc;
			totalSources = xml.ba.totalSources;
			totalSpecies = xml.ba.totalSpecies;
			species = new ArrayCollection();
			for each (var s:Object in xml.ba.species.list.source.item) {
				var sp:Species = new Species();
				sp.gbifId = s.gbifId;
				sp.group = s.group;
				sp.id = s.id;
				sp.scientificName = s.scientificName;
				sp.totalCells = s.totalCells;
				sp.totalOcc = s.totalOcc;
				for each(var o:Object in s.cellOccs.list.source.item) {
					var occ:CellOcc = new CellOcc();
					occ.cellId = o.cellId;
					occ.count = o.count;
					occ.source = o.source;
					sp.cellOccs.addItem(occ);
				}
				species.addItem(sp);
			}
				
		}
		
	}
}